package com.yunzone.cloud.modules.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yunzone.cloud.common.entity.Result;
import com.yunzone.cloud.modules.common.CustomerBlockHandler;
import com.yunzone.cloud.modules.payment.entity.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "byResourceBlockHandler")
    public Result<String> byResource(){
        return new Result(200,true,"按照资源名称限流OK",new Payment(99L,"11111"));
    }

    public Result<String> byResourceBlockHandler(BlockException e){
        return new Result(444,false,e.getClass().getCanonicalName()+"服务不可用");
    }

    @GetMapping("/byURL")
    @SentinelResource(value = "byURL")
    public Result<String> byURL(){
        return new Result(200,true,"按照URL限流OK",new Payment(99L,"11111"));
    }

    @GetMapping("/customerHandler")
    @SentinelResource(value = "customerHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "customerHandler2")
    public Result<String> customerHandler(){
        return new Result(200,true,"customerHandler限流OK",new Payment(99L,"11111"));
    }

}
